/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamen.gesplan.vistes;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Roger
 */
public class Staff_Delete extends javax.swing.JFrame implements KeyListener {

    private boolean newInfo = false;
    private boolean closed = false;
    private String[] llista;
    private Object[] selected;
    
    /**
     * Creates new form Staff_Delete
     */
    public Staff_Delete() {
        initComponents();
        
        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension finestra = getSize();
        this.setLocation((pantalla.width - finestra.width) / 2,(pantalla.height - finestra.height) / 2);
        setVisible(true);
        setResizable(false);
        this.setTitle("Esborrar empleat");
        
        buscador2.addKeyListener(this);
    }

    public boolean isClosed() {
        return closed;
    }

    public void setClosed(boolean closed) {
        this.closed = closed;
    }

    public boolean newInfo() {
        return newInfo;
    }

    public void setNewInfo(boolean newInfo) {
        this.newInfo = newInfo;
    }

    public void setList(DefaultComboBoxModel model) {
        
        /* El mètode de domini ens retorna un 
         * DefaultComboBoxModel, però el component necessita
         * les dades com a String[], així que fem la conversió
         */
        
        llista = new String[model.getSize()];
        for (int i=0; i < model.getSize(); ++i) {
            llista[i] = (String) model.getElementAt(i);
        }
        buscador.establecerLista(llista);
    }
    
    public void setJList(DefaultComboBoxModel model) {
        
        /* El mètode de domini ens retorna un 
         * DefaultComboBoxModel, però el component necessita
         * les dades com a String[], així que fem la conversió
         */
        
        llista = new String[model.getSize()];
        for (int i=0; i < model.getSize(); ++i) {
            llista[i] = (String) model.getElementAt(i);
        }
        llista_possibles.setModel(model);
    }

    public String getSelected() {
        return buscador.getText();
    }

    public String[] getListSelected() {
        String[] res = new String[selected.length];
        for (int i=0; i < res.length; ++i) {
            res[i] = selected[i].toString();
        }
        return res;
    }
    
    
    
    public void infoMessage(int res) {
        if (res == 0) {
            JOptionPane.showMessageDialog(this, "No existeix aquest empleat", 
                    "Error d'eliminació", JOptionPane.ERROR_MESSAGE);
        }
        else {
            JOptionPane.showMessageDialog(this, "S'han eliminat " + res + " empleats", 
                    "Eliminació confirmada", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        esborrar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        buscador = new LuisPlugin.LTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        llista_possibles = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        buscador2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(365, 365));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        esborrar.setText("Esborra");
        esborrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                esborrarActionPerformed(evt);
            }
        });

        jLabel1.setText("Busca per cognom:");

        jScrollPane1.setViewportView(llista_possibles);

        jLabel2.setText("Busca per cognom:");

        buscador2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscador2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(buscador2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                    .addComponent(esborrar)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(buscador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(253, 253, 253))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buscador2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(esborrar)
                .addGap(37, 37, 37))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void esborrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_esborrarActionPerformed
        Object[] opcions = {"Sí", "No"};
        
        /* Demanem confirmació per esborrar, 0 = Sí, 1 = No */
        int res = JOptionPane.showOptionDialog(this, "Segur que vols esborrar l'empleat " 
                + buscador.getText() + "?", "Confirma eliminació", JOptionPane.YES_NO_CANCEL_OPTION,
                JOptionPane.WARNING_MESSAGE, null , opcions, opcions[1]);
        
        if (res == 0) {
            newInfo = true;
            
            selected = llista_possibles.getSelectedValues();
            
            /* tot aquest codi de sota es pel buscador1, amb el buscador2 actualitzem 
             * la llista cridant a BD
             */
            
            /* Ara hem d'actualitzar la llista */
            List newList = new ArrayList();
            /* Creem una llista auxiliar on estan tots els elemnents menys l'esborrat */
            for (int i=0; i < llista.length; ++i) {
                if (!llista[i].equals(buscador.getText())) newList.add(llista[i]);
            }
            /* Fem la còpia a la llista original */
            llista = new String[newList.size()];
            for (int i=0; i < llista.length; ++i) {
                llista[i] = (String) newList.get(i);
            }
            /* Establim la llista */
            buscador.establecerLista(llista);
        }
        else newInfo = false;
        
    }//GEN-LAST:event_esborrarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        closed = true;
    }//GEN-LAST:event_formWindowClosing

    private void buscador2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscador2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_buscador2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private LuisPlugin.LTextField buscador;
    private javax.swing.JTextField buscador2;
    private javax.swing.JButton esborrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList llista_possibles;
    // End of variables declaration//GEN-END:variables

    public void keyTyped(KeyEvent e) {
        
    }

    public void keyPressed(KeyEvent e) {
        
    }

    public void keyReleased(KeyEvent e) {
        if (e.getSource()==buscador2) {
            String ini = buscador2.getText();
            DefaultListModel results = new DefaultListModel() {};
            for (int i=0; i < llista.length; ++i) {
                if (llista[i].startsWith(ini)) results.addElement(llista[i]);
            }
            llista_possibles.setModel(results);
        }
    }

    
}
